function communication(){
	//private
	var SERVER_SMESSAGE 	= 1;
	var SERVER_UMESSAGE 	= 2;
	var SERVER_USER_LIST 	= 3;
	var SERVER_PLAYER 		= 4;
	var USER_REGISTER 	= 1;
	var USER_MESSAGE 	= 2;
	var USER_LIST 		= 3;
	var USER_PLAYER 	= 4;
	//public
	this.Server;
	this.UserName = "";
	
	//private methods
	function log(text,event) {
		if(event==1)
			text = "<font color='#AAAAAA'>"+text+"</font>";
		$log = $('#content');
		//Add text to log
		$log.append(($log.html()?"<br>":'')+text);
		//Autoscroll
		$log[0].scrollTop = $log[0].scrollHeight - $log[0].clientHeight;
	}
	
	function send(event, text) {
		Server.send( 'message', event+"||"+text );
	}
	
	function dump(arr,level) {
		var dumped_text = "";
		if(!level) level = 0;
		
		//The padding given at the beginning of the line.
		var level_padding = "";
		for(var j=0;j<level+1;j++) level_padding += "    ";
		
		if(typeof(arr) == 'object') { //Array/Hashes/Objects 
			for(var item in arr) {
				var value = arr[item];
				
				if(typeof(value) == 'object') { //If it is an array,
					dumped_text += level_padding + "'" + item + "' ...\n";
					dumped_text += dump(value,level+1);
				} else {
					dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
				}
			}
		} else { //Stings/Chars/Numbers etc.
			dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
		}
		return dumped_text;
	}
	
	//public methods
	this.register = function(){
		if($('#Name').val()==""){
			alert("Your name can't be null!");
			return;
		}
		log("Registering...",1);
		UserName = $('#Name').val();
		send(USER_REGISTER, UserName);
		this.get_user_list();
		$("#register").hide();
		$("#registed").html(UserName);
		$("#registed").show();
	}
	
	this.send_message = function(){
		send(USER_MESSAGE, $('#talk').val());
		$('#talk').val('');
	}
	
	this.get_user_list = function(){
		send(USER_LIST, "");
	}
	
	this.sync = function(time){
		send(USER_PLAYER, time);
	}
	
	this.connect = function(site){
		log('Connecting...',1);
		Server = new FancyWebSocket(site);

		Server.bind('open', function() {
			log("Connect success!",1);
		});

		Server.bind('close', function( data ) {
			log("Disconnected.",1);
		});

		//Log any messages sent from server
		Server.bind('message', function( payload ) {
			var message = payload.split('||');
			if(message[0]==SERVER_SMESSAGE){
				log("<b><font color='blue'>SERVER</font></b>: "+message[1]);
			}
			else if(message[0]==SERVER_UMESSAGE){
				log("<b>"+message[2]+"</b>: "+message[1]);
			}
			else if(message[0]==SERVER_USER_LIST){
				var users = "";
				for(i=2;i<2+eval(message[1]);i++)
					users += "<option value='"+message[i]+"'>"+message[i]+"</option>";
				$("#users").html(users);
			}
			else if(message[0]==SERVER_PLAYER){
				player.seek(message[1]);
			}
			else
				return;
		});

		Server.connect();
	}
}